import requests
import json
import tornado.ioloop
import tornado.web


url_songlist = """http://songsearch.kugou.com/song_search_v2?
callback=jQuery112409052687354255996_1523620150750
&keyword=%s&page=1&pagesize=30&userid=-1&clientver=
&platform=WebFilter&tag=em&filter=2&iscorrection=1
&privilege_filter=0&_=1523620150762"""

song_url="http://www.kugou.com/yy/index.php?r=play/getdata&hash=%s"

class GetSongList(tornado.web.RequestHandler):
    def post(self):
        songname = self.get_argument('songname')
        r = requests.get(url_songlist % (songname))
        obj = json.loads(r.content)
        res = [{'FileName': item['FileName'], 'FileHash': item['FileHash'],
            'SingerName': item['SingerName'], "Duration": item['Duration']}
            for item in obj["data"]["lists"] ]
        self.finish({"res": res})

class GetSongUrl(tornado.web.RequestHandler):
    def post(self):
        filehash = self.get_argument('filehash')
        r = requests.get(song_url % (filehash))
        obj = json.loads(r.content)
        self.finish({"img": obj["data"]["img"], "play_url": obj["data"]["play_url"]})
    


def make_app():
    return tornado.web.Application([
        (r"/getSongList", GetSongList),
        (r"/getSongUrl", GetSongUrl),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(9000)
    tornado.ioloop.IOLoop.current().start()
    
  
